import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:get/get.dart';

import 'package:media_kit/media_kit.dart';
import 'package:media_kit_video/media_kit_video.dart';

// 视频组件
class ComponentVideo extends StatefulWidget {
  final String path;
  const ComponentVideo({super.key, required this.path});

  @override
  createState() => _ComponentVideoState();
}

class _ComponentVideoState extends State<ComponentVideo> {
  late final player = Player();
  late final controller = VideoController(player);

  @override
  void initState() {
    super.initState();
    _initVideo();
  }

  /*
   * 初始化视频 
   */
  _initVideo() async {
    await player.open(Media(widget.path));
  }

  @override
  void dispose() {
    player.dispose();
    SystemChrome.setPreferredOrientations([
      DeviceOrientation.portraitUp,
      DeviceOrientation.portraitDown,
    ]);
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    // 视频宽高
    Size size = MediaQuery.of(context).size;
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Colors.black,
        toolbarHeight: 0,
      ),
      body: Container(
        color: Colors.black,
        child: Stack(
          children: [
            SizedBox(
              width: size.width,
              height: size.height,
              child: Video(controller: controller),
            ),
            Positioned(
              top: 0,
              left: 10,
              child: IconButton(
                iconSize: 28,
                icon: const Icon(Icons.close),
                color: Colors.white,
                onPressed: () {
                  Get.back();
                },
              ),
            ),
          ],
        ),
      ),
    );
  }
}
